package com.ux.control
{
	import com.ux.abstract.ADragBar;
	import com.ux.event.DragBarEvent;
	import com.ux.util.BitmapUtil;
	import com.ux.util.MathUtil;
	
	import flash.display.MovieClip;
	
	public class DragBarVertical extends ADragBar
	{
		
		public function DragBarVertical()
		{
			super();
		}
		
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// 실제 버튼 움직임 제어 부분
		//
		//-----------------------------------------------------------------------------------
		
		override public function btnMove( value:Number = 0, isDrag:Boolean=false ):void
		{
			if( _enableScroll == false ) return;
			
			if( isDrag == true ) this.virtualBtnPoint.y += ( this.stage.mouseY - this._downPoint );
			else this.virtualBtnPoint.y += value;
			
			if( this.virtualBtnPoint.y < this.track.y ) this.virtualBtnPoint.y = this.track.y;
			else if( this.virtualBtnPoint.y > this.track.height + this.track.y - this.btn.height ) this.virtualBtnPoint.y = this.track.height + this.track.y - this.btn.height;
			
			if( scrollInterval > 0 )
			{
				var originNum : Number = this.virtualBtnPoint.y - this.track.y;
				
				if( Math.round( ( originNum % scrollInterval ) / scrollInterval ) == 0 )
				{
					this.btn.y = originNum-( originNum % scrollInterval ) + this.track.y;
				}
				else
				{
					this.btn.y = originNum-( originNum % scrollInterval ) + this.track.y + scrollInterval;
				}
			}
			else
			{
				this.btn.y = this.virtualBtnPoint.y;
			}
			
			if( this.btn.y < this.track.y ) this.btn.y = this.track.y;
			else if( this.btn.y > this.track.height + this.track.y - this.btn.height ) this.btn.y = this.track.height + this.track.y - this.btn.height;
			
			
			var maxNum : Number = this.track.height + this.track.y - this.btn.height;
			var minNum : Number = this.track.y;
			var statusNum : Number = this.btn.y + this.track.y;
			
			var rate : Number = MathUtil.NumberToRate( maxNum, minNum, statusNum );
			
			setIconPosition();
			dispatchEvent( new DragBarEvent( DragBarEvent.VERTICAL_UPDATE, rate ) );
		}
		
		override protected function setDownPoint():void
		{
			this._downPoint = this.stage.mouseY;
		}
		
		override protected function setIconPosition():void
		{
			if( _icon )
			{
				_icon.y = _btn.y+( _btn.height - _icon.height )/2;
				_icon.x = _btn.x+( _btn.width - _icon.width )/2;
				setChildIndex( _icon , numChildren-1 );
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// api
		//
		//-----------------------------------------------------------------------------------
		
		override public function get track():MovieClip 
		{
			if( this._track == null )
			{
				this._track = new MovieClip();
				this._track.addChild( BitmapUtil.MakeBitmap( 20, 300, 0xcccccc ) ); 
			}
			
			return this._track;
		}
		
		override public function get btn():MovieClip 
		{
			if( this._btn == null )
			{
				this._btn = new MovieClip();
				this._btn.addChild( BitmapUtil.MakeBitmap( 20, 40, 0x000000 ) ); 
			}
			return this._btn;
		}
		
		override public function btnReSize(contentsSize:Number):void
		{
			var rate : Number = MathUtil.NumberToRate( contentsSize, 0, this.track.height );
			if( rate > 1 ) rate = 1;
			this.btn.getChildAt( 0 ).height = Math.max( 20, MathUtil.RateToNumber( this.track.height, 0 , rate ) );
			setIconPosition();
		}
		
		
		override public function reset():void
		{
			this.btnMove( -btn.y );
		}
		
		public function set enableScroll( value:Boolean ):void
		{
			_enableScroll = value;
		}
		
		public function get enableScroll():Boolean
		{
			return _enableScroll;
		}
		
		
	}
}